package cn.wikiflyer.beikang.act.home;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.bean.HomeBean.HomeNewsBean;
import cn.wk.libs4a.utils.WKTimeUtils;
import cn.wk.libs4a.view.list.WKBaseAdapter;

/**
 * Home咨询的数据适配器
 */
public class HomeListAdapter extends WKBaseAdapter {

	private ArrayList<HomeNewsBean> data;

	public HomeListAdapter(Context context) {
		super(context);
	}

	public void setData(ArrayList<HomeNewsBean> data) {
		this.data = data;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (data != null) {
			return data.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder mViewHolder = null;
		if (convertView == null) {
			mViewHolder = new ViewHolder();
			convertView = LayoutInflater.from(ctx).inflate(
					R.layout.act_home_news_item, null);
			mViewHolder.img_logo = (ImageView) convertView
					.findViewById(R.id.img);
			mViewHolder.tv_name = (TextView) convertView
					.findViewById(R.id.name);
			mViewHolder.tv_part = (TextView) convertView
					.findViewById(R.id.partment);
			mViewHolder.tv_time = (TextView) convertView
					.findViewById(R.id.time);
			convertView.setTag(mViewHolder);
		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}
		HomeNewsBean mHomeNewsBean = data.get(position);
		mViewHolder.tv_name.setText(mHomeNewsBean.title);
		mViewHolder.tv_part.setText(mHomeNewsBean.description);
		mViewHolder.tv_time.setText(WKTimeUtils
				.formatTime(mHomeNewsBean.updatetime));

		app().imageLoader.displayImage(mHomeNewsBean.thumb,
				mViewHolder.img_logo);
		final IntentBean mIBean = new IntentBean(mHomeNewsBean.title,
				mHomeNewsBean.thumb, mHomeNewsBean.description,
				mHomeNewsBean.inputtime, mHomeNewsBean.catid, mHomeNewsBean.id);

		convertView.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Intent i = new Intent(ctx, NewsAct.class);
				i.putExtra("title", "倍康资讯");
				i.putExtra("bean", mIBean);
				ctx.startActivity(i);
			}
		});
		return convertView;
	}

	class ViewHolder {
		ImageView img_logo;
		TextView tv_name;
		TextView tv_part;
		TextView tv_time;
	}

}
